﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using forum.Data;

namespace forum.ApplicationCode
{
    public class ListManager
    {

        public static List<ForumItem> GetSubForumList()
        {
            List<ForumItem> list = new List<ForumItem>();
            using (forumDataContext data = new forumDataContext())
            {
                foreach (var subForum in data.sp_GetSubForumList())
                {
                    list.Add(new ForumItem(subForum.subForumTopic, subForum.subForumId.ToString()));
                }
            }
            return list;
        }

        public static List<ForumItem> GetNewSubForumModerators()
        {
            List<ForumItem> list = new List<ForumItem>();
            using (forumDataContext data = new forumDataContext())
            {
                foreach (var user in data.sp_GetNewSubForumModerators())
                {
                    list.Add(new ForumItem(user.userName, user.userId.ToString()));
                }
            }
            return list;
        }


        public static List<ForumItem> GetModeratorsBySubForum(string subForumName)
        {
            List<ForumItem> list = new List<ForumItem>();
            using (forumDataContext data = new forumDataContext())
            {
                foreach (var user in data.sp_GetModeratorsBySubForum(subForumName))
                {
                    list.Add(new ForumItem(user.userName, user.userId.ToString()));
                }
            }
            return list;
        }

        public static List<ForumItem> GetSubForumCandidateModerators(string subForumName)
        {
            
            List<ForumItem> list = new List<ForumItem>();
            using (forumDataContext data = new forumDataContext())
            {
                int subForumId = data.sp_GetSubForumIdByName(subForumName).FirstOrDefault().subForumId;
                foreach (var user in data.sp_GetAllUsers())
                {
                    // check if user post more than 5 massages in sub forum
                    int numOfPosts = data.sp_GetUserNumberOfPostInSubForum(subForumId,user.userId).FirstOrDefault().numOfPosts.Value;
                    int numOfThreads = data.sp_GetUserNumberOfThreadsInSubForum(subForumId, user.userId).FirstOrDefault().numOfThreads.Value;
                    // check if not already moderator
                    Boolean isModerator = false ; 
                    if (data.sp_IsUserModerator(user.userId, subForumId).FirstOrDefault().isModerator > 0)
                        isModerator = true;
                    if ( (numOfPosts+numOfThreads) >5 && !isModerator)
                      list.Add(new ForumItem(user.userName, user.userId.ToString()));
                }
            }
            return list;
        }

        public static List<ForumItem> GetFirstForumModerators()
        {
            List<ForumItem> list = new List<ForumItem>();
            using (forumDataContext data = new forumDataContext())
            {
                var firstForum = data.sp_GetSubForumList().FirstOrDefault();
                if (firstForum!=null)
                {

                    int firstForumId = firstForum.subForumId;

                    foreach (var user in data.sp_GetSubForumModeratoes(firstForumId))
                         {
                    list.Add(new ForumItem(user.userName, user.userId.ToString()));
                          }
                }
            }
            
            return list;
        }
        
        public static List<ForumItem> GetFirstForumCandidateList()
        {
            List<ForumItem> list = new List<ForumItem>();
            using (forumDataContext data = new forumDataContext())
            {
               

                var firstForum = data.sp_GetSubForumList().FirstOrDefault();
                if (firstForum != null)
                {
                    int firstForumId = firstForum.subForumId;

                    foreach (var user in data.sp_GetAllUsers())
                    {
                        // check if user post more than 5 massages in sub forum
                        int numOfPosts = data.sp_GetUserNumberOfPostInSubForum(firstForumId, user.userId).FirstOrDefault().numOfPosts.Value;
                        int numOfThreads = data.sp_GetUserNumberOfThreadsInSubForum(firstForumId, user.userId).FirstOrDefault().numOfThreads.Value;
                        Boolean isModerator = false;
                        if (data.sp_IsUserModerator(user.userId, firstForumId).FirstOrDefault().isModerator > 0)
                            isModerator = true;
                        if ((numOfPosts + numOfThreads) > 5 && !isModerator)
                            list.Add(new ForumItem(user.userName, user.userId.ToString()));
                    }
                }
            }
            return list;
        }

        public static List<ForumItem> GetUserList()
        {
            List<ForumItem> list = new List<ForumItem>();
            using (forumDataContext data = new forumDataContext())
            {
                foreach (var user in data.sp_GetAllUsers())
                {
                    list.Add(new ForumItem(user.userName, user.userId.ToString()));
                }
            }
            return list;
        }



        public static List<ForumItem> GetCandidateBannedUsers(int subForumId)
        {
            List<ForumItem> list = new List<ForumItem>();
            using (forumDataContext data = new forumDataContext())
            {
                foreach (var user in data.sp_GetCandidateBannedUsers(subForumId))
                {
                    list.Add(new ForumItem(user.userName, user.userId.ToString()));
                }
            }
            return list;
        }


        public static object GetUserSubForumModerator(int userId)
        {
            List<ForumItem> list = new List<ForumItem>();
            using (forumDataContext data = new forumDataContext())
            {
                foreach (var forum in data.sp_GetUserModerateList(userId))
                {
                    list.Add(new ForumItem(forum.subForumTopic, forum.subForumId.ToString()));
                }
            }
            return list;
        }

        internal static List<ForumItem> GetBannedUsers(int subForumId)
        {
            List<ForumItem> list = new List<ForumItem>();
            using (forumDataContext data = new forumDataContext())
            {
                foreach (var user in data.sp_GetBannedUsers(subForumId))
                {
                    list.Add(new ForumItem(user.userName, user.userId.ToString()));
                }
            }
            return list;
        }
    }

     public class ForumItem
        {
            public string _Text
            {
                get;
                set;
            }
            public string _Value
            {
                get;
                set;
            }

            public ForumItem()
            {
                _Text = "";
                _Value = "";
            }

            public ForumItem(string text, string value)
            {
                _Text = text;
                _Value = value;
            }
        }
}